﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xc.Application;
using Xc.DataObjects;

namespace Permissions.Web.Controllers
{
    public class AccountController : BaseController
    {
        //
        // GET: /Account/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit()
        {
            return View();
        }

        [HttpPost]
        public JsonResult UserLogin(LoginModel login)
        {
            var result = base.GetModelValidate(login);
            if (result == null)
            {
                AccountAppService appService = new AccountAppService();
                return this.Json(appService.CheckAccount(login.UserName, login.Password));
            }
            return this.Json(result);
        }

        public JsonResult GetGridData()
        {
            AccountAppService service = new AccountAppService();
            var request = new JqGridRequest(HttpContext);
            var jqGrid = service.GetAllAccount(request.PageIndex, request.PageSize);
            return this.Json(jqGrid);
        }

        public JsonResult Create(AccountDataObject entity)
        {
            var result = base.GetModelValidate(entity);
            if (result == null)
            {
                AccountAppService service = new AccountAppService();
                return this.Json(service.AddAccount(entity));
            }
            return this.Json(result);
        }

        public JsonResult Update(AccountDataObject entity)
        {
            var result = base.GetModelValidate(entity);
            if (result == null)
            {
                AccountAppService service = new AccountAppService();
                return this.Json(service.UpdateAccount(entity));
            }
            return this.Json(result);
        }

        public JsonResult Delete(AccountDataObject entity)
        {
            AccountAppService service = new AccountAppService();
            return this.Json(service.DeleteAccount(entity));
        }


        public JsonResult CheckAccountIsExists(AccountDataObject entity)
        {
            AccountAppService service = new AccountAppService();
            var result = service.CheckAccoutIsExists(entity.Name, entity.Id);
            var data = new { success = !result, message = result ? "已存在该用户" : "" };
            return this.Json(data);
        }

        public JsonResult ResetPassword(AccountDataObject entity)
        {
            AccountAppService service = new AccountAppService();
            return this.Json(service.ResetPassword(entity));
        }

        public JsonResult GetAccountRole(Guid? accountGuid)
        {
            AccountAppService service = new AccountAppService();
            if (accountGuid != null) return this.Json(service.GetAccountRoles(accountGuid.Value));
            return null;
        }
    }
}